<?php
require_once 'util/class.dbservice.php';

class UserDao {
	
	 public function getUserBasicInfo($userId) {
	 	try {
	 		$collection = $this->getCollection();
	 		if(!$collection)
	 			return null;
	 		
	 		return $collection->findOne(array('_id'=>$userId),
	 					array('_id'=>1, 'nick'=>1, 'avatar'=>1, 'profile'=>1,
	 					'noteNum'=>1, 'followerNum'=>1, 'followeeNum'=>1,
	 					'favorateNum'=>1));
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "UserDao.getUserBasicInfo error, reason=".$e);
	 		return null;
	 	}
	 }
	 
	 public function getUserBasicInfoByname($name) {
	 	try {
	 		$collection = $this->getCollection();
	 		if(!$collection)
	 			return null;
	 
	 		return $collection->findOne(array('nick'=>$name),
	 				array('_id'=>1, 'nick'=>1, 'avatar'=>1, 'profile'=>1,
	 						'noteNum'=>1, 'followerNum'=>1, 'followeeNum'=>1,
	 						'favorateNum'=>1));
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "UserDao.getUserBasicInfo error, reason=".$e);
	 		return null;
	 	}
	 }
	 
	 public function randomRecommendUser($num) {
	 	try {
	 		$collection = $this->getCollection();
	 		if(!$collection)
	 			return null;
	 		
	 		$start = 0;
	 		$end = 0;
	 		$totalNum = $collection->count();
	 		if($totalNum>$num)
	 			$end = $totalNum-$num;
	 		$pos = rand($start, $end);
	 		$cursor = $collection->find(array(),
	 				array('_id'=>1, 'nick'=>1, 'avatar'=>1, 'profile'=>1,
	 					'noteNum'=>1, 'followerNum'=>1, 'followeeNum'=>1,
	 					'favorateNum'=>1))->skip($pos)->limit($num);
	 		$result=array();
	 		foreach ($cursor as $doc) {
	 			$result[]=$doc;
	 		}
	 		return $result;
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "UserDao.randomRecommendUser error, reason=".$e);
	 		return null;
	 	}
	 }
	 
	 public function addUser($userId, $nick, $password, $email, $src, $ip, $ts) {
	 	try {
	 		$collection = $this->getCollection();
	 		if(!$collection)
	 			return null;
	 	
	 		return $collection->insert(array('_id'=>$userId, 'nick'=>$nick, 'pwd'=>$password, 
	 						'email'=>$email, 'src'=>$src, 'ip'=>$ip, 'lastLogin'=>$ts));
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "UserDao.addUser error, reason=".$e);
	 		return null;
	 	}
	 }
	 
	 public function getUserByNick($nick) {
	 	try {
	 		$collection = $this->getCollection();
	 		if(!$collection)
	 			return null;
	 	
	 		return $collection->findOne(array('nick'=>$nick),array('nick'=>1));
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "UserDao.getUserByNick error, reason=".$e);
	 		return null;
	 	}
	 }
	 
	 public function searchUserByNick($pattern, $offset, $limit) {
	 	try {
	 		$collection = $this->getCollection();
	 		if(!$collection)
	 			return null;
	 
	 		$cursor = $collection->find(array('nick'=> array('$regex' =>$pattern)),
	 				array('_id'=>1, 'nick'=>1, 'avatar'=>1, 'profile'=>1,
	 					'noteNum'=>1, 'followerNum'=>1, 'followeeNum'=>1,
	 					'favorateNum'=>1))->skip($offset)->limit($limit);
	 		$result=array();
	 		foreach ($cursor as $doc) {
	 			$result[]=$doc;
	 		}
	 		return $result;
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "UserDao.searchUserByNick error, reason=".$e);
	 		return null;
	 	}
	 }
	 
	 
	 
	 public function getUserByEmail($email) {
	 	try {
	 		$collection = $this->getCollection();
	 		if(!$collection)
	 			return null;
	 	
	 		return $collection->findOne(array('email'=>$email),array('email'=>1, 'nick'=>1, 'pwd'=>1));
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "UserDao.getUserByEmail error, reason=".$e);
	 		return null;
	 	}
	 }
	 
	 public function getUserNickAndPwd($userId) {
	 	try {
	 		$collection = $this->getCollection();
	 		if(!$collection)
	 			return null;
	 
	 		return $collection->findOne(array('_id'=>$userId),array('nick'=>1, 'pwd'=>1));
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "UserDao.getUserNickAndPwd error, reason=".$e);
	 		return null;
	 	}
	 }
	 
	 public function getUserDisplayInfo($userId) {
	 	try {
	 		$collection = $this->getCollection();
	 		if(!$collection)
	 			return null;
	 	
	 		return $collection->findOne(array('_id'=>$userId),
	 				array('_id'=>1, 'nick'=>1, 'avatar'=>1));
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "UserDao.getUserDisplayInfo error, reason=".$e);
	 		return null;
	 	}
	 }
	 
	 public function updateField($userId, $fieldName, $value) {
	 	try {
	 		$collection = $this->getCollection();
	 		if(!$collection)
	 			return;
	 	
	 		$collection->update(array('_id'=>$userId),
	 				array('$set'=>array($fieldName=>$value)),
	 				array('upsert'=>true));
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "UserDao.updateField error, reason=".$e);
	 	}
	 }
	 
	 public function updateFields($userId, $values) {
	 	try {
	 		$collection = $this->getCollection();
	 		if(!$collection)
	 			return;
	 
	 		$collection->update(array('_id'=>$userId),
	 				array('$set'=>$values),
	 				array('upsert'=>true));
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "UserDao.updateFields error, reason=".$e);
	 	}
	 }
	 
     private function getCollection() {
	 	$db=DBService::getGlobalDb();
	 	return $db->user;
	 }
}